//
//  ViewController.m
//  one
//
//  Created by admin on 2023/10/11.
//

#import "ViewController.h"
#import "NetworkTools.h"
#import "RSAUtil.h"



@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    [self.username setText:@"ZHPS_Admin"];
//    [self.pwd setText:@"Rehome.zhps@996"];
    //李耿
    [self.username setText:@"310261"];
    [self.pwd setText:@"!LLg770731"];
    
    
    
}


- (IBAction)btnClick:(id)sender {
    NSLog(@"haha");
    [self postLogin];
    //[self testPostTwo];
    
}

-(void)testPost{
    
    NSString *param = @"ALmSSvL4sBL9VMXMJH4IznebXiVzHhAsnC9r2CbZ7m5xudJoQcv7JjbHIJ0vuriZLgpw64lWpHjTqD98NnpA+PdALyduJvuhVQmNfWDb65y/VanUJuNC3q7VnOOU1d4aFgDZEb81oz0uBSbVeNBRU4Q8/SfA/PsToy9GDCd/sSL8/ApLzK+c4yywgoktwkYW8awAhmex8kTRRb3oRq0vWPpfryRl8G/L8M1h02bX7VR3fy1bPzZHpAbR2cRYqOMD4Gg/FTI8cMonh/jaSS/ADcJEsslESk8WZ8w7b5PJAvBQyUBud4fHIaLwjBY7pbs9brrAQnTjUwG/u09KdZmMNw==";
    
    NSString *urlString = @"PubFile/Data/AppLogin/GetDataRsa.aspx";
    
    [[NetworkTools sharedTools] request:POST URLString:urlString parameters: param finished:^(id  _Nonnull result, NSError * _Nonnull error) {
        if(error != nil){
            
            return;
        }else{
            
        }
        NSLog(@"%@",result);
    }];
}

-(void)postLogin{
    NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                       @"1d8404185d72d804",@"imeinum",
                                       self.pwd.text,@"password",
                                       @"iPhone16",@"phonemodel",
                                       @"",@"phonenum",
                                       @"16",@"sysversion",
                                       self.username.text,@"username",  nil];
    
    NSMutableDictionary *mutableDicParam = [[NSMutableDictionary alloc] init];
    mutableDicParam[@"Rows"]=@[mutableDic];
    
    NSLog(@"mutableDic :%@",mutableDicParam);
    
    BOOL isYes = [NSJSONSerialization isValidJSONObject:mutableDicParam];
    if(isYes){
        NSLog(@"可以转换");
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:mutableDicParam options:0 error:NULL];
        NSString *_rsaStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        NSLog(@"%@",_rsaStr);
        NSString *param = [RSAUtil encryptString:_rsaStr publicKey:RSA_Public_key];
        NSLog(@"%@",param);
        
        NSString *urlString = @"PubFile/Data/AppLogin/GetDataRsa.aspx";
        
        [[NetworkTools sharedTools] request:POST URLString:urlString parameters: param finished:^(id  _Nonnull result, NSError * _Nonnull error) {
            if(error != nil){
                
                return;
            }else{
                
            }
            NSString *resultStr = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
            NSString *resultDecode = [RSAUtil decryptString:resultStr privateKey:RSA_Privite_key];
            
            //NSLog(@"%@",result);
            NSLog(@"%@",resultDecode);
            
            NSData *dataDecode = [resultDecode dataUsingEncoding:NSUTF8StringEncoding];
            NSDictionary *dictDecode = [NSJSONSerialization JSONObjectWithData:dataDecode options:NSJSONReadingMutableContainers error:nil];
            NSArray *arryRow = dictDecode[@"Rows"];
            NSDictionary *dictRow = arryRow[0];
            NSString *token = dictRow[@"token"];
            NSLog(@"%@",token);
            [self getDataByApi:token];
            
        }];
        
    }else{
        NSLog(@"JSON数据生成失败，请检查数据格式");
    }
}

-(void)testPostOne{
    //    NSString *urlString = @"http://itunes.apple.com/search?term=metallica";
    //        NSURL *url = [NSURL URLWithString:urlString];
    //
    //        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //        [manager GET:url.absoluteString parameters:nil progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
    //
    //            NSLog(@"results: %@", responseObject);
    //
    //        } failure:^(NSURLSessionDataTask *task, NSError *error) {
    //
    //            NSLog(@"results: %@", error);
    //
    //        }];
    
    NSString *urlString = @"http://192.168.2.215:8082/PubFile/Data/AppLogin/GetDataRsa.aspx";
    NSURL *url = [NSURL URLWithString:urlString];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json",@"text/javascript",@"text/html",nil];
    
    
    //        [manager GET:url.absoluteString parameters:nil progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
    //
    //            NSLog(@"results: %@", responseObject);
    //
    //        } failure:^(NSURLSessionDataTask *task, NSError *error) {
    //
    //            NSLog(@"results: %@", error);
    //
    //        }];
    
    
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
    //manager.requestSerializer =  [AFJSONRequestSerializer serializer];
    NSString *param = @"ALmSSvL4sBL9VMXMJH4IznebXiVzHhAsnC9r2CbZ7m5xudJoQcv7JjbHIJ0vuriZLgpw64lWpHjTqD98NnpA+PdALyduJvuhVQmNfWDb65y/VanUJuNC3q7VnOOU1d4aFgDZEb81oz0uBSbVeNBRU4Q8/SfA/PsToy9GDCd/sSL8/ApLzK+c4yywgoktwkYW8awAhmex8kTRRb3oRq0vWPpfryRl8G/L8M1h02bX7VR3fy1bPzZHpAbR2cRYqOMD4Gg/FTI8cMonh/jaSS/ADcJEsslESk8WZ8w7b5PJAvBQyUBud4fHIaLwjBY7pbs9brrAQnTjUwG/u09KdZmMNw==";
    
    
    [manager POST:url.absoluteString parameters:param success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSLog(@"results: %@", responseObject);
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        NSLog(@"results: %@", error);
        
    }];
    
}

-(void)testPostTwo{
    
    NSString *urlString = @"http://192.168.2.215:8082/PubFile/Data/AppLogin/GetDataRsa.aspx";
    NSURL *url = [NSURL URLWithString:urlString];
    
    NSString *param = @"ALmSSvL4sBL9VMXMJH4IznebXiVzHhAsnC9r2CbZ7m5xudJoQcv7JjbHIJ0vuriZLgpw64lWpHjTqD98NnpA+PdALyduJvuhVQmNfWDb65y/VanUJuNC3q7VnOOU1d4aFgDZEb81oz0uBSbVeNBRU4Q8/SfA/PsToy9GDCd/sSL8/ApLzK+c4yywgoktwkYW8awAhmex8kTRRb3oRq0vWPpfryRl8G/L8M1h02bX7VR3fy1bPzZHpAbR2cRYqOMD4Gg/FTI8cMonh/jaSS/ADcJEsslESk8WZ8w7b5PJAvBQyUBud4fHIaLwjBY7pbs9brrAQnTjUwG/u09KdZmMNw==";
    
    
    NSData* data = [param dataUsingEncoding:NSUTF8StringEncoding];
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url
                                                               cachePolicy:NSURLRequestReloadIgnoringCacheData
                                                           timeoutInterval:1000];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:data];
    
    NSURLSession *requestSessions = [NSURLSession sharedSession];
    NSURLSessionDataTask *requestTasks = [requestSessions dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        //NSLog(@"data ======= %@, response ======= %@, error ======= %@",data ,response, error);
        
        if(error==NULL){
            NSString *resultStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"data ======= %@",resultStr);
        }
    }];
    [requestTasks resume];
}

-(void)getDataByApi:(NSString *) token{
    
    //https://mobile.zhp.geg.com.cn:7082/PubFile/Data/aboutme/aboutdetailRsa.ashx
    
    //PubFile/Data/GetOrderListRsa.ashx?departmentnameid=090300&page=1&pageSize=20&listcode=ApplySuggestion&proposerid=&manid=310261
    
    //NSString *urlString = @"PubFile/Data/aboutme/aboutdetailRsa.ashx";
    NSString *urlString = @"PubFile/Data/GetOrderListRsa.ashx?departmentnameid=090300&page=1&pageSize=20&listcode=ApplySuggestion&proposerid=&manid=310261";
    
    
    
    [[NetworkTools sharedTools] request:GET URLString:urlString parameters: nil token:token finished:^(id  _Nonnull result, NSError * _Nonnull error) {
        if(error != nil){
            
            return;
        }else{
            
        }
        NSString *resultStr = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
        NSString *resultDecode = [RSAUtil decryptString:resultStr privateKey:RSA_Privite_key];
        
        NSLog(@"%@",result);
        NSLog(@"%@",resultDecode);
        
//        NSData *dataDecode = [resultDecode dataUsingEncoding:NSUTF8StringEncoding];
//        NSDictionary *dictDecode = [NSJSONSerialization JSONObjectWithData:dataDecode options:NSJSONReadingMutableContainers error:nil];
//        NSArray *arryRow = dictDecode[@"Rows"];
//        NSDictionary *dictRow = arryRow[0];
//        NSString *token = dictRow[@"token"];
//        NSLog(@"%@",token);
        
    }];
    
}

@end
